﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace D2DEngine
{
    public abstract class D2DComponent : ICloneable
    {
        protected bool isRegistered = false;
        protected D2DObject owner;

        /// <summary>
        /// The object altered by this component
        /// </summary>
        public D2DObject Owner
        {
            get { return this.owner; }
            set { this.owner = value; }
        }        

        /// <summary>
        /// Is the component registered to a scene ?
        /// </summary>
        public bool IsRegistered
        {
            get { return this.isRegistered; }
            set { this.isRegistered = value; }
        }

        public D2DComponent()
        {
        }

        /// <summary>
        /// Uninitializes the component.
        /// Component can not process anymore.
        /// </summary>
        public void Dispose()
        {
            this.isRegistered = false;
        }

        /// <summary>
        /// Updates the component
        /// </summary>
        /// <param name="gameTime"></param>
        public virtual void Update(GameTime gameTime)
        {
        }

        /// <summary>
        /// Draws the component
        /// </summary>
        /// <param name="spriteBatch"></param>
        public virtual void Draw(SpriteBatch spriteBatch)
        {
        }

        /// <summary>
        /// Operations to execute when the component registers its owner.
        /// </summary>
        public virtual void OnRegister()
        {
            this.isRegistered = true;
        }

        /// <summary>
        /// Operations to execute when the component is unregistered
        /// </summary>
        public virtual void OnUnRegister()
        {

        }
        
        /// <summary>
        /// Clones the component
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            D2DComponent component = (D2DComponent)Activator.CreateInstance(this.GetType());

            CopyTo(component);

            return component;
        }
        

        /// <summary>
        /// Copies the component
        /// </summary>
        /// <param name="component"></param>
        protected virtual void CopyTo(D2DComponent component)
        {
        }
    }
}
